<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
use classlib\validator\auditor;
class Cchartlet extends controller {
	public function main() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$dlang = C\lang::inst('dd');
		$usage = \src\logic\lconfig::dd('system_charlets_usage');
		$view->assign_by_ref('use', $usage);
		$view->assign('status', $dlang->get('sh0006'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/chartlet.ext.js');
	}
	
	/**
	 * @desc    保存贴图资料信息
	 * @doc     编辑/添加贴图
	 * @access  需要授权 
	 */
	public function dosave() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$filter = new \classlib\validator\auditor();
		$lang   = C\lang::inst('adm');
		$filter->lang($lang);
		$data   = array();
		try {
			$data['notes']  = $filter->reset('notes')->maxlen(63)->run();
			$data['status'] = $filter->reset('status')->numeric()->run();
			$data['rank']   = $filter->reset('rank')->numeric()->run();
			$data['use']    = $filter->reset('use')->numeric()->run();
			$acl  = \classlib\acl::inst();
			$res  = \src\logic\upfile::inst($acl)->post('Fileicon');
			if ($res) {
				$data['icon'] = $res['url'];
			}
			$res  = \src\logic\upfile::inst($acl)->post('Filesrc');
			if ($res) {
				$data['src'] = $res['url'];
			}
		} catch (\classlib\validator\AuditorException $e) {
			$view->assign_error($e->getField(), $e->getMessage());
		} catch (\Exception $e) {
			$view->assign_error($e->getCode(), $e->getMessage());
		}
		$orm   = C\model::load('pro.inc.chartlet');
		$id    = (int)input::get_post('id');
		($id > 0)? $orm->save($id, $data):$orm->new_one($data);
		Cadmin::log2db(C\view::inst('xml')->array2xml($data));
		$view->display();
	}
	
	/**
	 * @desc    删除等级配置
	 * @doc     删除记录
	 * @access  需要授权
	 */
	public function dodelete() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids    = auditor::intstr2arr('ids');
		$orm    = C\model::load('pro.inc.chartlet');
		foreach ($ids as $id) {
			$data  = $orm->get_one($id);
			if ($data) {
				Cadmin::log2db(C\view::inst('xml')->array2xml($data));
				$orm->delete($id);
			}
		}
		$view->display();
	}
	
	/**
	 * 获取资料列表
	 */
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$orm    = C\model::load('pro.inc.chartlet');
		$where  = array();
		$status = input::get_post('status');
		if (is_numeric($status)) {
			$where[] = array('status', $status);
		}
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total > 0) {
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}